package basic.sort;

import java.util.Arrays;

/**
 * @创建人 MBY381
 * @创建时间 2022/3/28 16:45
 * @描述 暂无
 */
public class A_SelectSort {
    /**
     * @param nums
     * @return
     */
    public static int[] selectSort(int[] nums) {
        int len = nums.length;
        int min;
        int tag = 0;
        for (int i = 0; i < len; i++) {
            min = nums[i];
            for (int j = i + 1; j < len; j++) {
                if (nums[j] < min) {
                    min = nums[j];
                    tag = j;
                }
            }
            if (min == nums[i]) {
                continue;
            }
            replace(nums, min, i, tag);
        }
        return nums;
    }

    public static void replace(int[] nums, int i, int start, int end) {
        for (int j = end; j > start; j--) {
            nums[j] = nums[j - 1];
        }
        nums[start] = i;
        return;
    }


    public static void sort(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            int min = i;//最小元素的下标
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;//找最小值
                }
            }
            //交换位置
            int temp = arr[i];
            arr[i] = arr[min];
            arr[min] = temp;
        }
    }


    public static void main(String[] args) {
        int[] nums = new int[]{0xE3, 381, 8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        System.out.println("正确答案：    [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 227, 259, 381, 696, 782]");
        System.out.println(Arrays.toString(selectSort(nums)));
        int[] nums1 = new int[]{0xE3, 381, 8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        sort(nums1);
        System.out.println("更好的解法" + Arrays.toString(nums1));
    }
}
